package com.yanqu.road.server.cmd.player;

import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_SEARCH_PLAYER, desc = "搜索玩家")
public class SearchPlayerCmd extends AbstractCommand {

	private long userAliasId;
	private long serverId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PlayerProto.SearchPlayerReqMsg reqMsg = PlayerProto.SearchPlayerReqMsg.parseFrom(packet.getBytes());
		userAliasId = reqMsg.getUserAliasId();
		serverId = reqMsg.getServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(0 == serverId || serverId == GameServer.getInstance().getServerId()) {//搜本服
			UserInfo userInfo = UserMgr.getUserInfoByAliasId(userAliasId);
			PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(userInfo);
			PlayerProto.SearchPlayerRespMsg.Builder respMsg = PlayerProto.SearchPlayerRespMsg.newBuilder();
			if (null != userMsg) {
				respMsg.setRet(0);
				respMsg.setPlayerData(userMsg);
			} else {
				respMsg.setRet(GameErrorCode.E_PLAYER_NO_FOUND);
			}
			player.sendPacket(Protocol.U_SEARCH_PLAYER, respMsg);
		}else {
			if(!ServerListMgr.isServerProvideServices(serverId)){
				ret = GameErrorCode.E_SEARCH_SERVER_MAINTAIN;
				PlayerProto.SearchPlayerRespMsg.Builder respMsg = PlayerProto.SearchPlayerRespMsg.newBuilder();
				respMsg.setRet(ret);
				player.sendPacket(Protocol.U_SEARCH_PLAYER, respMsg);
				return;
			}
			PlayerProto.SearchPlayerReqMsg.Builder msg = PlayerProto.SearchPlayerReqMsg.newBuilder();
			msg.setUserAliasId(userAliasId);
			msg.setServerId(serverId);
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_SEARCH_PLAYER, msg);
			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {

	}

}
